<template>
    <div>
        <div class="container">
            <div class="left-content">
                <el-input placeholder="输入关键字进行过滤" v-model="filterText"></el-input>
                <el-tree
                    class="filter-tree"
                    :data="data"
                    :props="defaultProps"
                    default-expand-all
                    draggable
                    :filter-node-method="filterNode"
                    @node-click="onNodeClick"
                    :highlight-current="true"
                    ref="tree"
                ></el-tree>
            </div>
            <div class="right-content">
                <div class="handle-box">
                    <el-input v-model="query.name" placeholder="请输入用户名称" class="handle-input mr10"></el-input>
                    <el-select
                        v-model="query.address"
                        placeholder="请选择状态"
                        class="handle-select mr10"
                    >
                        <el-option key="1" label="启用" value="启用"></el-option>
                        <el-option key="2" label="停用" value="停用"></el-option>
                    </el-select>
                    <el-button type="primary" icon="el-icon-search" @click="onSubmit"></el-button>
                    <el-button type="primary" @click="onOpenPersonDig">新增</el-button>
                    <el-button type="primary" @click="handleEdit">修改</el-button>
                    <el-button type="primary" @click="onDel">删除</el-button>
                </div>
                <el-table
                    :data="tableData"
                    border
                    class="table"
                    ref="multipleTable"
                    header-cell-class-name="table-header"
                    @selection-change="handleSelectionChange"
                    @row-click="clickRow"
                    @row-dblclick="dblclickRow"
                >
                    <el-table-column prop="id" label="#" width="55" align="center"></el-table-column>
                    <el-table-column type="selection" width="55" align="center"></el-table-column>
                    <el-table-column prop="name" label="用户名称" align="center"></el-table-column>
                    <el-table-column prop="loginId" label="用户账号" align="center"></el-table-column>
                    <el-table-column prop="hrName" label="人员名称" align="center"></el-table-column>
                    <el-table-column prop="departmentName" label="部门" align="center"></el-table-column>
                    <el-table-column prop="organName" label="公司" align="center"></el-table-column>
                    <el-table-column prop="createDate" label="创建日期" align="center"></el-table-column>
                    <el-table-column label="状态" width="80" align="center">
                        <template slot-scope="scope">
                            <el-tag v-show="scope.row.status===1">启用</el-tag>
                            <el-tag v-show="scope.row.status===0" type="warning">停用</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column prop="remark" label="备注" align="center"></el-table-column>
                    <!-- <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                    </el-table-column>-->
                </el-table>
                <div class="pagination">
                    <el-pagination
                        background
                        layout="total, prev, pager, next"
                        :current-page="query.pageIndex"
                        :page-size="query.pageSize"
                        :total="pageTotal"
                        @current-change="handlePageChange"
                    ></el-pagination>
                </div>
            </div>
        </div>
        <!-- 编辑弹出框 -->
        <userUpdate ref="userUpdate" @renderTable="getHrPerson" />
    </div>
</template>

<script>
import { fetchData } from '../../api/index';
import userUpdate from '@/components/views/userUpdate';

export default {
    // name: 'basetable',
    components: {
        userUpdate
    },
    data() {
        return {
            query: {
                address: '',
                name: '',
                pageIndex: 1,
                pageSize: 10
            },
            tableData: [
                {
                    id: 1,
                    name: '测试',
                    loginId: '001',
                    hrName: '张三',
                    departmentName: '研发部',
                    organName: '山泰',
                    createDate: '2020-10-10',
                    status: 1
                }
            ],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            form: {},
            idx: -1,
            id: 1,
            filterText: '',
            data: [
                {
                    id: 1,
                    label: '机构',
                    children: [
                        {
                            id: 4,
                            label: '机构1'
                        },
                        {
                            id: 5,
                            label: '机构2'
                        },
                        {
                            id: 6,
                            label: '机构3'
                        },
                        {
                            id: 7,
                            label: '机构4'
                        },
                        {
                            id: 8,
                            label: '机构5'
                        }
                    ]
                }
            ],
            defaultProps: {
                children: 'children',
                label: 'label'
            }
        };
    },
    created() {
        this.getData();
    },
    methods: {
        // 查询
        getHrPerson() {},
        // 新增按钮
        onOpenPersonDig() {
            this.$refs.userUpdate.show('');
        },
        // 编辑操作
        handleEdit() {
            console.log("updateShow");
            if (this.multipleSelection.length === 0) {
                this.$message({ message: '请先选择要修改的数据！', type: 'warning' });
            } else if (this.multipleSelection.length > 1) {
                this.$message({ message: '只能选择一条数据', type: 'warning' });
            } else {
                this.$refs.userUpdate.updateShow(this.multipleSelection[0]);
            }
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    this.$message.success('删除成功');
                    this.tableData.splice(index, 1);
                })
                .catch(() => {});
        },
        // 获取 easy-mock 的模拟数据
        getData() {
            // fetchData(this.query).then((res) => {
            //     // this.tableData = res.list;
            //     this.pageTotal = res.pageTotal || 50;
            // });
        },
        // 触发搜索按钮
        handleSearch() {
            // this.$set(this.query, 'pageIndex', 1);
            // this.getData();
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    this.$message.success('删除成功');
                    this.tableData.splice(index, 1);
                })
                .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        // 保存编辑
        saveEdit() {
            this.editVisible = false;
            this.$message.success(`修改第 ${this.idx + 1} 行成功`);
            this.$set(this.tableData, this.idx, this.form);
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        },
        // 增行
        onAdd() {
            this.id++;
            let newObj = {
                id: this.id,
                systemBrand: '',
                systemName: '',
                version: '',
                deploymentStyle: '',
                dataBaseType: '',
                dataBaseVersion: '',
                databaseName: '',
                dataBaseAddress: '',
                userName: '',
                password: ''
            };
            this.tableData.push(newObj);
        },
        // 删行
        onDel() {
            if (this.multipleSelection.length === 0) {
                this.$message({
                    message: '请选择要删除的数据',
                    type: 'warning'
                });
            } else {
                this.$confirm('确定要删除吗？', '提示', {
                    type: 'warning'
                })
                    .then(() => {
                        this.$message.success('删除成功');
                        this.id--;
                        this.multipleSelection.forEach((ele) => {
                            this.tableData.forEach((item, index) => {
                                ele.id === item.id && this.tableData.splice(index, 1);
                            });
                        });
                        console.log('this.tableData', this.tableData);
                    })
                    .catch(() => {});
            }
        },
        // 插行
        onInsert() {
            if (this.multipleSelection.length === 0) {
                this.$message({
                    message: '请选择一条记录',
                    type: 'warning'
                });
            } else {
                this.id++;
                let newObj = {
                    id: this.id,
                    systemBrand: '',
                    systemName: '',
                    version: '',
                    deploymentStyle: '',
                    dataBaseType: '',
                    dataBaseVersion: '',
                    databaseName: '',
                    dataBaseAddress: '',
                    userName: '',
                    password: ''
                };
                this.tableData.unshift(newObj);
            }
        },
        // 保存
        onSubmit() {
            this.$message({
                message: '获取成功！',
                type: 'success'
            });
        },
        filterNode(value, data) {
            if (!value) return true;
            return data.label.indexOf(value) !== -1;
        },
        // 单击行事件
        clickRow(row) {
            this.$refs.multipleTable.toggleRowSelection(row);
        },
        // 双击行事件
        dblclickRow(row, column, event) {
            console.log('dblclickRow', row, column, event);
            // this.handleEdit();
            this.$refs.userUpdate.updateShow(row);
        },
        // 树节点点击
        onNodeClick(data) {
            // console.log('onNodeClick data', data);
            // this.condition.departmentId = data.id;
            // this.getHrPerson();
        }
    },
    watch: {
        filterText(val) {
            this.$refs.tree.filter(val);
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
.container {
    display: flex;
    justify-content: left;
}
.left-content {
    width: 20%;
    height: 100%;
    margin-right: 5px;
}
.right-content {
    width: 80%;
    height: 100%;
}
</style>
